/*     */ package com.finir.finknjig.persistence.hibernate;
/*     */ 
/*     */ import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.finir.finknjig.util.HibernateUtil;
/*     */ @SuppressWarnings("unchecked")
/*     */ public class HibernatePersistenceManager
/*     */ {
/*     */   public void save(Object objectToSave)
/*     */   {
/*  19 */     HibernateUtil.beginTransaction();
/*     */ 
/*  21 */     HibernateUtil.getSession().save(objectToSave);
/*     */ 
/*  23 */     HibernateUtil.commitTransaction();
/*     */   }
/*     */ 
/*     */   public void mergeAndUpdate(Object objectToSave) {
/*  27 */     HibernateUtil.beginTransaction();
/*     */ 
/*  29 */     HibernateUtil.getSession().update(HibernateUtil.getSession().merge(objectToSave));
/*     */ 
/*  31 */     HibernateUtil.commitTransaction();
/*     */   }
/*     */ 
/*     */   public void mergeAndDelete(Object objectToDelete) {
/*  35 */     HibernateUtil.beginTransaction();
/*     */ 
/*  37 */     HibernateUtil.getSession().delete(HibernateUtil.getSession().merge(objectToDelete));
/*     */ 
/*  39 */     HibernateUtil.commitTransaction();
/*     */   }
/*     */ 
/*     */   public void saveOrUpdate(Object objectToSave) {
/*  43 */     HibernateUtil.beginTransaction();
/*  44 */     HibernateUtil.getSession().saveOrUpdate(objectToSave);
/*  45 */     HibernateUtil.commitTransaction();
/*     */   }
/*     */ 
/*     */   public void delete(Object objectToDelete) {
/*  49 */     HibernateUtil.beginTransaction();
/*  50 */     HibernateUtil.getSession().delete(objectToDelete);
/*  51 */     HibernateUtil.commitTransaction();
/*     */   }
/*     */ 
/*     */   public <T> List<T> getAll(Class<T> type)
/*     */   {
/*  57 */     HibernateUtil.beginTransaction();
/*     */ 
/*  59 */     Criteria criteria = HibernateUtil.getSession().createCriteria(type);
/*     */ 
/*  61 */     HibernateUtil.commitTransaction();
/*  62 */     return criteria.list();
/*     */   }
/*     */ 
/*     */   public <T> List<T> get(Class<T> searchType, int firstReslult, int lastResult)
/*     */   {
/*  67 */     HibernateUtil.beginTransaction();
/*  68 */     Criteria criteria = HibernateUtil.getSession().createCriteria(searchType);
/*  69 */     criteria.setMaxResults(lastResult);
/*  70 */     criteria.setFirstResult(firstReslult);
/*  71 */     HibernateUtil.commitTransaction();
/*  72 */     return criteria.list();
/*     */   }
/*     */ 
/*     */   
public <T> T findByID(Class<T> type, Serializable id)
/*     */   {
/*  79 */     HibernateUtil.beginTransaction();
/*  80 */     Criteria criteria = HibernateUtil.getSession().createCriteria(type);
/*  81 */     criteria.add(Restrictions.idEq(id));
/*     */ 
/*  83 */     HibernateUtil.commitTransaction();
/*  84 */     return (T) criteria.uniqueResult();
/*     */   }
/*     */ 
/*     */   public void deleteAll(Class type)
/*     */   {
/*  95 */     List list = getAll(type);
/*  96 */     for (Iterator localIterator = list.iterator(); localIterator.hasNext(); ) { Object object = localIterator.next();
/*  97 */       delete(object);
/*     */     }
/*     */   }
/*     */ 
/*     */   public <T> List<T> executeCriteria(Class<T> clazz, DetachedCriteria detachedCriteria)
/*     */   {
/* 105 */     HibernateUtil.beginTransaction();
/*     */ 
/* 107 */     Criteria criteria = detachedCriteria
/* 108 */       .getExecutableCriteria(HibernateUtil.getSession());
/*     */ 
/* 110 */     HibernateUtil.commitTransaction();
/* 111 */     return criteria.list();
/*     */   }
/*     */ 
/*     */   public <T> List<T> executeQuery(Class<T> clazz, String hql, List param)
/*     */   {
/* 118 */     HibernateUtil.beginTransaction();
/* 119 */     int index = 0;
/* 120 */     Query query = HibernateUtil.getSession().createQuery(hql);
/* 121 */     for (Iterator localIterator = param.iterator(); localIterator.hasNext(); ) { Object object = localIterator.next();
/* 122 */       query.setParameter(index++, object);
/*     */     }
/*     */ 
/* 125 */     HibernateUtil.commitTransaction();
/* 126 */     return query.list();
/*     */   }
/*     */ 
/*     */   public <T> T returnUniqueResult(Class<T> ret, String hql, List param)
/*     */   {
/* 132 */     HibernateUtil.beginTransaction();
/* 133 */     Query query = HibernateUtil.getSession().createQuery(hql);
/* 134 */     int index = 0;
/* 135 */     for (Iterator localIterator = param.iterator(); localIterator.hasNext(); ) { Object object = localIterator.next();
/* 136 */       query.setParameter(index++, object);
/*     */     }
/*     */ 
/* 139 */     HibernateUtil.commitTransaction();
/* 140 */     return (T) query.uniqueResult();
/*     */   }
/*     */ 
/*     */   public Session getManager() {
/* 144 */     return HibernateUtil.getSession();
/*     */   }
/*     */ }


